package com.gsafety.libraryktx.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.OvalShape
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.gsafety.libraryktx.R

/**
 * @author : lj
 * @date : 2020/7/31 18:25
 */
class LegendText@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
                                          defStyleAttr: Int = 0) : LinearLayout(context,attrs,defStyleAttr)  {
    private var tvName:TextView?=null
    private var bg:TextView?=null
    private var tvValue:TextView?=null
    init {
        initview(attrs)
    }

    private fun initview(attrs:AttributeSet?){
        var view = LayoutInflater.from(context).inflate(R.layout.common_legend_layout, this)
        var typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.common_LegendText)
        var bgcolor = typedArray.getColor(R.styleable.common_LegendText_common_roundColor, resources.getColor(R.color.common_over_blue))
        bg=view.findViewById(R.id.rt_bg)
        tvName=view.findViewById(R.id.tv_name)
        tvValue=view.findViewById(R.id.tv_value)
        var value = typedArray.getString(R.styleable.common_LegendText_common_text)
        tvName!!.text=value
        typedArray.recycle()
        setbg(bgcolor)
    }

    fun setTitle(name:String){
        tvName?.let {
            it.text=name
        }
    }

    fun setbg(color: Int){
        bg?.let {
//            it.setBackgroundColor(color)
            var drawable = ShapeDrawable()
            drawable.shape = OvalShape()
            drawable.paint.color = color
            bg?.background = drawable
        }
    }

    fun setValue(value:String){
        tvValue?.let {
            it.text=value
            it.visibility = View.VISIBLE
        }
    }
}